<?php

/**
 *
 * @copyright 	Copyright (c) 2011, Globtec (http://www.globtec.com.br)
 * @author 		Jose Carlos Soares de Souza <josecarlos@globtec.com.br>
 */
class App_Plugins_Admin_Layout extends Zend_Controller_Plugin_Abstract
{
	/**
	 * 
	 * @var Zend_Controller_Plugin_Abstract
	 */
	protected $_request;

	/**
	 * 
	 * (non-PHPdoc)
	 * @see Zend_Controller_Plugin_Abstract::dispatchLoopStartup()
	 */
	public function dispatchLoopStartup ( Zend_Controller_Request_Abstract $request )
	{
		$this->_request = $request;
		
		$this->_configLayout();
		$this->_configView();
		
		$this->_configNavigation();
		$this->_includeJsController();
		$this->_includeCssController();
	}

	/**
	 * @access protected
	 * @return void
	 */
	protected function _configView ()
	{
		$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
		
		$view->docType('HTML5');
		
		$view->headTitle('Administrador - ' . Zend_Registry::get('config')->geral->title);
		
		$view->headMeta()->appendName('charset', 'utf-8');
		
		//Favicon
		$view->headLink(array('rel' => 'shortcut icon', 'type' => 'image/x-icon', 'href' => $view->baseUrl('favicon.ico')));
		$view->headLink(array('rel' => 'icon', 'type' => 'image/png', 'href' => $view->baseUrl('favicon.png')));
		
		//Global stylesheets
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/reset.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/common.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/form.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/standard.css'), '');
		
		//Generic libs
		$view->headScript()->appendFile($view->baseUrl('public/scripts/jquery.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/html5.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/old-browsers.js'));
		
		//Template libs
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/common.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/standard.js'));
		
		// if IE8
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/standard.ie.js'),'text/javascript', array('conditional' => 'lte IE 8'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.tip.js'));
		
		if ( Zend_Auth::getInstance()->hasIdentity() )
			$this->_isLogged( $view );
		else
			$this->_isNotLogged( $view );
	}
	
	/**
	 * 
	 * @access protected
	 * @param Zend_View $view
	 * @return void
	 */
	protected function _isLogged ( Zend_View $view )
	{
		//Comment/uncomment one of these files to toggle between fixed and fluid layout
		//$view->headLink()->appendStylesheet( $view->baseUrl( 'public/styles/admin/960.gs.css' ) );
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/960.gs.fluid.css'), '');
		
		//Custom styles
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/simple-lists.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/block-lists.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/planning.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/table.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/calendars.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/wizard.css'), '');
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/gallery.css'), '');
		
		//Template libs
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.accessibleList.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/searchField.js'));
		
		// if IE8
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.hashchange.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.contextMenu.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.modal.js'));
		
		//Custom styles lib
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/list.js'));
		
		//Plugins 
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.dataTables.min.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/jquery.datepick/jquery.datepick.min.js'));
		$view->headScript()->appendFile( $view->baseUrl( 'public/scripts/jquery.maskedinput.js' ) );
		
		//Charts library / Load the AJAX API
		//$view->headScript()->appendFile( 'http://www.google.com/jsapi' );
		$view->headScript()->appendFile($view->baseUrl('public/scripts/ajaxupload.js'));
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/admin.js'));
	}
	
	/**
	 * 
	 * @access protected
	 * @param Zend_View $view
	 * @return void
	 */
	protected function _isNotLogged ( Zend_View $view )
	{
		$view->headLink()->appendStylesheet($view->baseUrl('public/styles/admin/special-pages.css'), '');
		
		$view->headScript()->appendFile($view->baseUrl('public/scripts/admin/login.js'));
	}

	/**
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _configLayout ()
	{
		Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH . '/modules/admin/layouts/');
	}

	/**
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _configNavigation ()
	{
		$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
		
		// Define controller active
		$controllerActive = $view->navigation()->findOneBy('controller', $this->_request->getControllerName());
		
		if ( $controllerActive ) 
			$controllerActive->setActive(true);
	}

	/**
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _includeJsController ()
	{
		$ds = DIRECTORY_SEPARATOR;
		
		$file = 'public' . $ds . 'scripts' . $ds . $this->_request->getModuleName() . $ds . 'controller' . $ds . $this->_request->getControllerName() . '.js';
		
		if ( file_exists(APPLICATION_PATH . $ds . '..' . $ds . $file) ) {
			
			$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
			
			$view->headScript()->appendFile($view->baseUrl($file));
			
		}
	}
	
	/**
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _includeCssController ()
	{
		$ds = DIRECTORY_SEPARATOR;
		
		$file = 'public' . $ds . 'styles' . $ds . $this->_request->getModuleName() . $ds . 'controller' . $ds . $this->_request->getControllerName() . '.css';
		
		if ( file_exists(APPLICATION_PATH . $ds . '..' . $ds . $file) ) {
			
			$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
			
			$view->headLink()->appendStylesheet($view->baseUrl($file));
			
		}
	}
}